package com.aracky.system.service.impl;

import com.aracky.common.exception.BaseException;
import com.aracky.system.model.entity.SysDictData;
import com.aracky.system.mapper.SysDictDataMapper;
import com.aracky.system.model.form.SysDictDataForm;
import com.aracky.system.service.SysDictDataService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author zengzhiping
 * @since 2022-07-15 09:11:40
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {

    @Override
    public void dictValueExists(SysDictDataForm sysDictDataForm) {
        //先判断当前字典类型对应的数据值是否已存在
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = Wrappers.<SysDictData>lambdaQuery()
                .eq(SysDictData::getDictId, sysDictDataForm.getDictId())
                .eq(SysDictData::getDictValue, sysDictDataForm.getDictValue())
                .ne(sysDictDataForm.getDataId() != null, SysDictData::getDataId, sysDictDataForm.getDataId());

        long count = this.count(lambdaQueryWrapper);
        if (count > 0) {
            throw  new BaseException("该字典类型下已存在相同的字典值");
        }
    }
}
